//
// Created on 2024/12/10.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef RLOTTIE_DEMO_IMAGENODE_H
#define RLOTTIE_DEMO_IMAGENODE_H

#include "ArkUINode.h"
#include "Getrlottie.h"
#include <multimedia/image_framework/image/pixelmap_native.h>

namespace NativeModule {

class ImageNode : public ArkUINode {
public:
    Glottie *Lalrlottie = new Glottie;
    std::string FileName = "/data/storage/el2/base/haps/entry/files/lottie_1_1.json";
    int32_t FrameNum = 0;
    float width_ = 0;
    float height_ = 0;

    ImageNode(float width, float height)
        : ArkUINode((NativeModuleInstance::GetInstance()->GetNativeNodeAPI())->createNode(ARKUI_NODE_IMAGE))
    {
//         nativeModule_->addNodeEventReceiver(handle_,OnStaticNodeEvent);
//         nativeModule_->registerNodeEvent(handle_,NODE_EVENT_ON_AREA_CHANGE,0,this);
        width_ = width * 2;
        height_ = height * 2;
        ArkUI_NumberValue valueWidth[] = {{.f32 = width}};
        ArkUI_AttributeItem itemWH = {valueWidth, 1};
        nativeModule_->setAttribute(handle_, NODE_WIDTH, &itemWH);
        ArkUI_NumberValue valueHeight[] = {{.f32 = height}};
        itemWH = {valueHeight, 1};
        nativeModule_->setAttribute(handle_, NODE_HEIGHT, &itemWH);

        Lalrlottie->InitFormFile(FileName);
        Lalrlottie->PrintInfo();
    }

    ~ImageNode() override
    {
//         nativeModule_->removeNodeEventReceiver(handle_,OnStaticNodeEvent);
//         nativeModule_->unregisterNodeEvent(handle_, NODE_EVENT_ON_AREA_CHANGE);
        delete Lalrlottie;
    }

    // 渲染一帧图像
    int32_t PlayFrame()
    {
        if (FrameNum >= 100) {
            FrameNum = 0;
        } else {
            FrameNum++;
        }


        OH_PixelmapNative *resPixMap = nullptr;

        uint8_t *resces = reinterpret_cast<uint8_t *>(Lalrlottie->RenderFrameTorgba(FrameNum, width_, height_));

        OH_Pixelmap_InitializationOptions *options;
        OH_PixelmapInitializationOptions_Create(&options);
        OH_PixelmapInitializationOptions_SetWidth(options, width_);
        OH_PixelmapInitializationOptions_SetHeight(options, height_);
        OH_PixelmapInitializationOptions_SetPixelFormat(options, PIXEL_FORMAT_RGBA_8888);

        auto now = std::chrono::high_resolution_clock::now();
        auto startTime = std::chrono::time_point_cast<std::chrono::nanoseconds>(now).time_since_epoch();
        
        OH_PixelmapNative_CreatePixelmap(resces, width_ * height_ * 4, options, &resPixMap);
        
        now = std::chrono::high_resolution_clock::now();
        auto endTime = std::chrono::time_point_cast<std::chrono::nanoseconds>(now).time_since_epoch();
        DRAWING_LOGE("\n\n\n---单次解析第%{public}2.d帧,用时%{public}5.2fms", FrameNum, (endTime - startTime) / 1000000.0);
        
        ArkUI_DrawableDescriptor *gerRes = OH_ArkUI_DrawableDescriptor_CreateFromPixelMap(resPixMap);

        void *bitmap = static_cast<void *>(gerRes);

        ArkUI_AttributeItem item = {.object = bitmap};

        nativeModule_->setAttribute(handle_, NODE_IMAGE_SRC, &item);


        return 1;
    }
};

} // namespace NativeModule

#endif // RLOTTIE_DEMO_IMAGENODE_H